/**
 * 
 */
package com.expense.servlet.sysMng;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import com.expense.entity.UserInfo;
import com.expense.services.UserInfoServices;
import com.expense.servlet.BasicServlet;

/**
 * @author Administrator
 * 用户 登陆 登出
 * @author xj
 */
public class LoginLogoutServlet extends BasicServlet {
	
	/**
	 * Constructor of the object.
	 */
	public LoginLogoutServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		HttpSession session=request.getSession();
		
		String opt=request.getParameter("opt");
		if(opt!=null){
			if(opt.equals("toLogin")){//到登陆
				String path="/login.jsp";
				RequestDispatcher dispatcher = request.getRequestDispatcher(path);
				dispatcher .forward(request, response);
			}else if(opt.equals("login")){//登陆
				UserInfoServices userInfoServices=new UserInfoServices();
				String loginName=request.getParameter("loginName");
				String passWord=request.getParameter("passWord");
				
				//UserInfo  userInfo=
					

				String loginString=userInfoServices.userLogin(loginName, passWord,request);
				if (loginString.equals("loginSuc")) {//登录成功
					logger.info("用户"+loginName+"登陆");
					response.sendRedirect("index.jsp");
				}else {
					String errInfo="";
					if (loginString.equals("errConnect")) {
						errInfo="数据库连接失败,请通知管理员!";
					}else if (loginString.equals("errUser")) {
						errInfo="没有此用户!";
					}else if (loginString.equals("errPassWord")) {
						errInfo="用户密码错误!";
					}
					request.setAttribute("errInfo", errInfo);
					
					String path="/loginErr.jsp";
					RequestDispatcher dispatcher = request.getRequestDispatcher(path);
					dispatcher .forward(request, response);
					
				} 
			}else if(opt.equals("logout")){//登出
				UserInfo  loginAdmin=this.getLoginAdmin(request);
				session.removeAttribute("loginAdmin");
				session.invalidate();
				if(loginAdmin!=null){
					logger.info("用户"+loginAdmin.getLoginName()+"登出");
				}
				response.sendRedirect("login.html");
			}
		}
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
